import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../components/LoginPage.vue'
import HomePage from '../components/HomePage.vue'
import OperationalAddComponent from '../components/OperationalAddComponent.vue'
import OperationalComponent from '../components/OperationalComponent.vue'
import Account from '../base/AccountStore'
import CategoryComponent from '../components/CategoryComponent.vue'
import GoodsManagerComponent from '../components/GoodsManagerComponent.vue'
import GoodsEditCompoent from '../components/GoodsEditCompoent.vue'
import OrderComponent from '../components/OrderComponent.vue'
import EvaluationComponent from '../components/EvaluationComponent.vue'
import StatisticalComponent from '../components/StatisticalComponent.vue'
// 定义页面名称枚举
enum PageName {
  login = 'login',
  home = 'home'
}

// 定义路由
const shopRouter = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: PageName.login,
      component: LoginPage
    },
    {
      path: '/home',
      name: PageName.home,
      component: HomePage,
      children: [
        {
          path: 'operational',
          component: OperationalComponent,
          name: "operational"
        },
        {
          path: 'operationaladd',
          component: OperationalAddComponent,
          name: "operationaladd"
        },
        {
          path: 'category',
          component: CategoryComponent,
          name: 'category'
        },
        {
          path: 'goodsmanager',
          component: GoodsManagerComponent,
          name: 'goodsmanager'
        },
        {
          path: 'goodsedit/:mode?/:id?',
          component: GoodsEditCompoent,
          name: 'goodsedit',
          props: true
        },
        {
          path: 'ordermanager',
          component: OrderComponent,
          name: 'ordermanager'
        },
        {
          path: 'evaluationManager',
          component: EvaluationComponent,
          name: "evaluationManager"
        },
        {
          path: 'statisticalManager',
          component: StatisticalComponent,
          name: "statisticalManager"
        }
      ],
      redirect: '/home/operational'
    }
  ]
});

// 定义前置守卫
shopRouter.beforeEach((to) => {
  // 如果访问的不是登录页，则重定向到登录页，如果没有登录则跳转到登录页
  if (to.name !== PageName.login && !Account().isLogin) {
    return {
      name: PageName.login
    }
  }
});

// 导出路由
export default shopRouter;
export { PageName }